package com.sunwayworld.basemodule.basedata.purchase.service.impl;

import com.sunwayworld.basemodule.basedata.purchase.bean.LimsPurchaseBean;
import com.sunwayworld.basemodule.basedata.purchase.bean.LimsPurchaseMaterialBean;
import com.sunwayworld.basemodule.basedata.purchase.dao.LimsPurchaseDao;
import com.sunwayworld.basemodule.basedata.purchase.service.LimsPurchaseMaterialService;
import com.sunwayworld.basemodule.basedata.purchase.service.LimsPurchaseService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsPurchaseServiceImpl implements LimsPurchaseService {

    @Autowired
    private LimsPurchaseDao limsPurchaseDao;

    @Autowired
    private LimsPurchaseMaterialService limsPurchaseMaterialService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsPurchaseDao getDao() {
        return limsPurchaseDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsPurchaseBean limsPurchase = jsonWrapper.parseUnique(LimsPurchaseBean.class);
        limsPurchase.setId(ApplicationContextHelper.getNextIdentity());
        limsPurchase.setApplyCode(ApplicationContextHelper.getNextSequence(getDao().getTable()));
        getDao().insert(limsPurchase);
        return limsPurchase.getId();
    }

    @Override
    public Page<LimsPurchaseMaterialBean> selectMaterialPaginationByPurchaseId(Long id, RestJsonWrapperBean wrapper) {
        return limsPurchaseMaterialService.selectPaginationByFilter(SearchFilter.instance().match("PURCHASEID", id).filter(MatchPattern.EQ), wrapper);
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insertMaterial(Long id, RestJsonWrapperBean wrapper) {

        List<LimsPurchaseMaterialBean> limsPurchaseMaterialList = wrapper.parse(LimsPurchaseMaterialBean.class);

        limsPurchaseMaterialList.forEach(e -> {
            e.setId(ApplicationContextHelper.getNextIdentity());
            e.setPurchaseId(id);
        });

        limsPurchaseMaterialService.getDao().insert(limsPurchaseMaterialList);

        return limsPurchaseMaterialList.get(0).getId();
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.DELETE)
    public void deleteMaterial(Long id, RestJsonWrapperBean wrapper) {
        limsPurchaseMaterialService.delete(wrapper);
    }

    @Override
    public String checkMaterials(RestJsonWrapperBean wrapper) {
        List<LimsPurchaseBean> purchaseBeans = wrapper.parse(LimsPurchaseBean.class);
        List<Long> ids = purchaseBeans.stream().map(LimsPurchaseBean::getId).collect(Collectors.toList());
        if (ids.isEmpty()) {
            return "";
        }
        List<LimsPurchaseMaterialBean> materialBeans = limsPurchaseMaterialService.selectListByFilter(SearchFilter.instance().match("purchaseId", ids).filter(MatchPattern.OR));
        List<LimsPurchaseBean> withoutMats = purchaseBeans.stream().filter(p -> materialBeans.stream().noneMatch(m -> m.getPurchaseId().equals(p.getId()))).collect(Collectors.toList());
        if (!withoutMats.isEmpty()) {
            return "采购单物资为空：<br/>".concat(withoutMats.stream().map(LimsPurchaseBean::getApplyCode).distinct().collect(Collectors.joining(",<br/>")));
        }
        return "";
    }
}
